package com.mscsoft.brainpower.server.action;

import java.io.*;
import javax.annotation.Resource;

import com.mscsoft.brainpower.server.dao.*;
import com.mscsoft.brainpower.server.domain.Msg;

import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UploadAction {

    @Resource
    MusicFileDao musicFileDao;
    @Resource
    MusicScoreFileDao musicScoreFileDao;
    @Resource
    ImageFileDao imageFileDao;

    @RequestMapping("/musicInsert")
    public Msg<?> musicInsert(String name) {
        File musicFile = new File("src\\main\\resources\\music\\皎洁的笑颜.wav");
        File musicScoreFile = new File("src\\main\\resources\\musicscore\\皎洁的笑颜.json");
        File imgFile = new File("src\\main\\resources\\music\\皎洁的笑颜.jpg");
        try {
            byte[] musicscore = IOUtils.toByteArray(new FileInputStream(musicScoreFile));
            byte[] music = IOUtils.toByteArray(new FileInputStream(musicFile));
            byte[] img = IOUtils.toByteArray(new FileInputStream(imgFile));
            musicFileDao.insertNewMusic(name, music);
            musicScoreFileDao.insertNewMusicScore(name, musicscore);
            imageFileDao.insertNewImg(name, img);
            return new Msg<>(200, "存入数据库成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Msg<>(500, "存入数据库失败");
        }
    }
}
